package com.jiexin.service;

import com.google.common.base.Preconditions;
import com.jiexin.beans.PageQuery;
import com.jiexin.beans.PageResult;
import com.jiexin.common.RequestHolder;
import com.jiexin.dao.PlmWorkFlowStatusCriterionMapper;
import com.jiexin.exception.ParamException;
import com.jiexin.model.PlmWorkFlowStatus;
import com.jiexin.model.PlmWorkFlowStatusCriterion;
import com.jiexin.model.SysAcl;
import com.jiexin.param.WorkFlowStatusCriterionParam;
import com.jiexin.param.WorkFlowStatusParam;
import com.jiexin.util.BeanValidator;
import com.jiexin.util.IpUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: Jay_Liu
 * @Description:
 * @Date: Created in 21:06 2018/5/21 0021
 * @Modified By:
 */
@Service
public class PlmWorkFlowStatusCriterionService {

    @Resource
    PlmWorkFlowStatusCriterionMapper criterionMapper;

    public void save(WorkFlowStatusCriterionParam param) {
        BeanValidator.check(param);
        //查询名称
        if(checkIsExist(param.getStatusId(), param.getName(), param.getId())){
            throw new ParamException("同一个关卡下不允许存在相同关卡标准名称");
        }
        PlmWorkFlowStatusCriterion criterion = PlmWorkFlowStatusCriterion.builder().name(param.getName()).statusId(param.getStatusId())
                .approver(param.getApprover()).conditions(param.getConditions()).remark(param.getRemark()).status(param.getStatus()).build();
        criterion.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
        criterion.setOperateTime(new Date());
        criterion.setOperator(RequestHolder.getCurrentUser().getUsername());
        criterionMapper.insertSelective(criterion);
    }

    public void update(WorkFlowStatusCriterionParam param) {
        BeanValidator.check(param);
        if(checkIsExist(param.getStatusId(), param.getName(), param.getId())){
            throw new ParamException("同一个关卡下不允许存在相同关卡标准名称");
        }
        PlmWorkFlowStatusCriterion before = criterionMapper.selectByPrimaryKey(param.getId());
        Preconditions.checkNotNull(before, "待更新的关卡标准不存在");
        PlmWorkFlowStatusCriterion after = PlmWorkFlowStatusCriterion.builder().id(param.getId()).name(param.getName()).statusId(param.getStatusId())
                .approver(param.getApprover()).conditions(param.getConditions()).remark(param.getRemark()).status(param.getStatus()).build();
        after.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
        after.setOperateTime(new Date());
        after.setOperator(RequestHolder.getCurrentUser().getUsername());
        criterionMapper.updateByPrimaryKeySelective(after);
    }

    public void delete(int workFlowStatusId) {
    }

    //检查是否存在相同名称的关卡名称
    private boolean checkIsExist(Integer workFlowStatusId, String name, Integer id) {
        return criterionMapper.countByNameAndWorkFlowStatusId(workFlowStatusId, name, id) > 0;
    }

    /**
     * 获取对应关卡下的分页关卡标准
     * @param workFlowStatusId 关卡ID
     * @param page 分页请求参数
     * @return 分页关卡标准
     */
    public PageResult<PlmWorkFlowStatusCriterion> getPageByStatusId(int workFlowStatusId, PageQuery page) {
        //检查分页参数
        BeanValidator.check(page);
        //获取当前关卡下的分页关卡标准
        int count = criterionMapper.countByWorkFlowStatusId(workFlowStatusId);
        if (count > 0) {
            List<PlmWorkFlowStatusCriterion> criterionList = criterionMapper.getPageByWorkFlowStatusId(workFlowStatusId, page);
            return PageResult.<PlmWorkFlowStatusCriterion>builder().data(criterionList).total(count).build();
        }
        return PageResult.<PlmWorkFlowStatusCriterion>builder().build();
    }
}
